package session;

import data.ObjectDatabase;

public class Session {

	private static Session instance;

	/*
	 * The name of the project for the current session.
	 */
	private String projectName;

	/*
	 * Creates a new session.
	 */
	private Session() {
		this.projectName = null;
	}

	/**
	 * The singleton accessor for the Session class.
	 * 
	 * @return Session The singleton instance of the application Core.
	 */
	public static Session getInstance() {
		if (instance == null) {
			instance = new Session();
		}
		return instance;
	}

	/**
	 * Loads a project into the current session. If a project is already loaded,
	 * close it before loading a new one.
	 * 
	 * @param projectName
	 *            The name of the project.
	 */
	public void loadProject(String projectName) {
		// Close the current project first if there is one
		if (this.projectName != null) {
			this.closeProject();
		}
		// Open and load
		this.projectName = projectName;
		ObjectDatabase.getInstance().load();
	}

	/**
	 * Saves and closes the current project. Only runs if there is a project
	 * currently loaded.
	 */
	public void closeProject() {
		// Only close if a project is loaded
		if (this.projectName != null) {
			// Save and close
			ObjectDatabase.getInstance().save();
			this.projectName = null;
		}
	}

	/**
	 * Returns the project name of the current session.
	 * 
	 * @return String The project name.
	 */
	public String getProjectName() {
		return this.projectName;
	}

}
